﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Newtonsoft.Json.Linq;
using WeChat.Common;
using BLL;

namespace WeChat.Manager
{
    public partial class MassManager : System.Web.UI.Page
    {
        GetReturnJson gr = new GetReturnJson();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ForLogin.IsLogin())
                {
                    
                }
                else
                {
                    Response.Redirect("../Login.aspx"); return;
                }
            }
        }

        //发送测试消息
        protected void sendMsgTest(object sender, EventArgs e)
        {
            string type = Request.Form["select1"].ToString();//素材类型
            switch (type)
            {
                case "图文素材": type = "mpnews"; break;
                case "图片素材": type = "image"; break;
                case "文本素材": type = "text"; break;
                case "语音素材": type = "voice"; break;
                default: type = "mpvideo"; break;//视频素材
            }
            string weixinh = Request["weixinh"].ToString();//测试微信号
            string mediaID = Request["mediaId1"].ToString();//素材ID或文本
            if (weixinh == "" || mediaID == "")
            {
                return;
            }
            //post发送数据并得到返回值
            string json = getJson(true, weixinh, type,mediaID);
            string accesstoken =  GetAccess_token.getAccesstoken();
            string url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=" + accesstoken;
            string rejson = gr.postReturnJson(url, json);

            JObject jo = JObject.Parse(rejson);
            string errcode = jo["errcode"].ToString();
            switch (errcode)
            {
                case "0":
                    Label1.Text = "发送成功！";
                    break;
                case "10432":
                    Label1.Text = "发送失败！微信号不合法";
                    break;
                case "40007":
                    Label1.Text = "发送失败！不合法的媒体文件id";
                    break;
                case "40003":
                    Label1.Text = "发送失败！不合法的openid";
                    break;
                default:
                    Label1.Text = "发送失败！errcode:" + errcode;
                    break;
            }
            Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>tips();</script>");
        }

        //群发消息
        protected void sendMsg(object sender, EventArgs e)
        {
            string type = selects.Items[selects.SelectedIndex].Value.ToString();
            switch (type)
            {
                case "图文素材": type = "mpnews"; break;
                case "图片素材": type = "image"; break;
                case "文本素材": type = "text"; break;
                case "语音素材": type = "voice"; break;
                default: type = "mpvideo"; break;//视频素材 
            }
            string media_id = mediaId2.Text.ToString();

            //string json = getJson(false, "", type, media_id);
            if (media_id == "") { return; }
            //else { return; }

            //post发送数据并得到返回值
            string json = getJson(false, "", type, media_id);
            ApplicationLog.Write("群发json：" + json);
            string accesstoken = GetAccess_token.getAccesstoken();
            string url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=" + accesstoken;
            string rejson = gr.postReturnJson(url, json);

            JObject jo = JObject.Parse(rejson);
            string errcode = jo["errcode"].ToString();
            switch (errcode)
            {
                case "0":
                    Label1.Text = "发送成功！";
                    break;
                case "40007":
                    Label1.Text = "发送失败！不合法的媒体文件id";
                    break;
                default:
                    Label1.Text = "发送失败！errcode:" + errcode;
                    break;
            }
            Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>tips();</script>");
        }


        //根据传入值,转换得到对应json
        private string getJson(bool test, string openid, string type, string media_id)
        {
            JObject jo = new JObject();
            if (test)
            {
                //jo.Add("towxname", toUsername);//发送测试用户
                jo.Add("touser", openid);
            }
            else
            {
                //jo.Add("filter", new JObject(new JProperty("is_to_all", false), new JProperty("group_id", "0")));//发送某个分组
                jo.Add("filter", new JObject(new JProperty("is_to_all", true)));//发送全部用户
            }
            if (type.Equals("text"))
            {
                jo.Add(type, new JObject(new JProperty("content", media_id)));
            }
            else
            {
                jo.Add(type, new JObject(new JProperty("media_id", media_id)));
            }
            jo.Add("msgtype", type);
            return jo.ToString();
        }

        //查看群发消息记录
        protected void showMassMsg(object sender, EventArgs e)
        {
            MediaBLL mb = new MediaBLL();
            massReports.DataSource = mb.getMassMsg();
            massReports.DataBind();
        }
    }
}
